#include "mainwindow.h"
#include "ui_mainwindow.h"

// 标题窗口初始化
void MainWindow::LeftWidget_TitleBtn_Init()
{
    DownIcon = new QIcon(":/image/down_gray.png");
    DownIcon_Hover = new QIcon(":/image/down_black.png");
    RightIcon = new QIcon(":/image/right_gray.png");
    RightIcon_Hover = new QIcon(":/image/right_black.png");
    //设置导航栏标题
	LeftWidget_TitleBtn_Set(ui->btn_title_f210, "   F210");
    LeftWidget_TitleBtn_Set(ui->btn_title_dlbox, "DL400Pro");
    LeftWidget_TitleBtn_Set(ui->btn_title_f316, "   F3xx");
    LeftWidget_TitleBtn_Set(ui->btn_title_dl8000, "  DLX000");
}

void MainWindow::LeftWidget_TitleBtn_Set(QPushButton* btn_title, QString TitleName)
{
	btn_title->setStyleSheet("text-align:left;font:bold 20px;");
	btn_title->setIcon(*DownIcon);
	btn_title->setText(TitleName);
	btn_title->installEventFilter(this);
}

// 左侧导航栏鼠标悬停事件
void MainWindow::LeftWidget_BtnTitle_SetIcon_Move(QPushButton* btn_title, bool* HoverStu)
{
    if(*HoverStu)
    {
        btn_title->setIcon(QIcon(*DownIcon_Hover));
    }
    else
    {
        btn_title->setIcon(QIcon(*RightIcon_Hover));
    }
}
// 左侧导航栏鼠标移开事件
void MainWindow::LeftWidget_BtnTitle_SetIcon_Leave(QPushButton* btn_title, bool* HoverStu)
{
    if(*HoverStu)
    {
        btn_title->setIcon(QIcon(*DownIcon));
    }
    else
    {
        btn_title->setIcon(QIcon(*RightIcon));
    }
}
// 左侧导航栏鼠标点击事件
void MainWindow::LeftWidget_BtnTitle_SetIcon_Release(QPushButton* btn_title, QList<QAbstractButton *> btn_group, bool* ReleaseStu)
{
    int count = btn_group.count();
    *ReleaseStu = !(*ReleaseStu);
    if(*ReleaseStu)
    {
        for (int i = 0; i < count; ++i) {
            QAbstractButton *btn = btn_group.at(i);
            btn->setVisible(true);
        }
        btn_title->setIcon(QIcon(*DownIcon_Hover));
    }
    else
    {
        for (int i = 0; i < count; ++i) {
            QAbstractButton *btn = btn_group.at(i);
            btn->setVisible(false);
        }
        btn_title->setIcon(QIcon(*RightIcon_Hover));
    }
}

// 左侧导航栏按钮组初始化
void MainWindow::Left_Layout_Btn_Init(QButtonGroup *btnGroup, bool textBesideIcon)
{
    QList<QAbstractButton *> btns = btnGroup->buttons();
    foreach (QAbstractButton *btn, btns) {
        QToolButton *b = (QToolButton *)btn;
        //关联按钮单击事件
        connect(b, SIGNAL(clicked(bool)), this, SLOT(Left_Layout_Btn_Click()));
        b->setCheckable(true);
        b->setToolButtonStyle(textBesideIcon ? Qt::ToolButtonTextBesideIcon : Qt::ToolButtonTextOnly);
    }
}
// 左侧导航栏按钮事件
void MainWindow::Left_Layout_Btn_Click()
{
    QAbstractButton *b = (QAbstractButton *)sender();
    QString name = b->text();

    int count = LeftBtns.count();
    for (int i = 0; i < count; ++i) {
        QAbstractButton *btn = LeftBtns.at(i);
        btn->setChecked(btn == b);
    }
}
// 左侧导航栏初始化
void MainWindow::Left_Widgt_Init()
{
    //加入按钮组自动互斥
    QButtonGroup *btnGroup = new QButtonGroup(this);
    btnGroup->addButton(ui->btn_left_mtsn_f210);
    btnGroup->addButton(ui->btn_left_bat_f210);
    btnGroup->addButton(ui->btn_left_disp_f210);
    btnGroup->addButton(ui->btn_left_mtsn_dlbox);
    btnGroup->addButton(ui->btn_left_cfg_dlbox);
    btnGroup->addButton(ui->btn_left_loop_f316);
    btnGroup->addButton(ui->btn_left_dl8000);

    LeftBtns.clear();
    LeftBtns<<ui->btn_left_mtsn_f210<<ui->btn_left_bat_f210<<ui->btn_left_disp_f210<<ui->btn_left_mtsn_dlbox<<ui->btn_left_cfg_dlbox<<ui->btn_left_loop_f316<<ui->btn_left_dl8000;
    LeftBtns_f210.clear();
#if 1
	LeftBtns_f210<<ui->btn_left_mtsn_f210;
#else
    LeftBtns_f210<<ui->btn_left_mtsn_f210<<ui->btn_left_bat_f210<<ui->btn_left_disp_f210;
#endif
    LeftBtns_dlbox.clear();
#if 1
    LeftBtns_dlbox<<ui->btn_left_mtsn_dlbox;
#else
    LeftBtns_dlbox<<ui->btn_left_mtsn_dlbox<<ui->btn_left_cfg_dlbox;
#endif
    LeftBtns_f316.clear();
    LeftBtns_f316<<ui->btn_left_loop_f316;
    LeftBtns_dl8000.clear();
    LeftBtns_dl8000<<ui->btn_left_dl8000;

    //设置按钮可选中以及图标样式
    int count = LeftBtns.count();
    for (int i = 0; i < count; ++i) {
        QToolButton *btn = (QToolButton *)LeftBtns.at(i);
        btn->setCheckable(true);
        btn->setToolButtonStyle(Qt::ToolButtonTextOnly);
    }

    //设置弱属性以便应用样式
    ui->wdg_left->setProperty("flag", "right");

    IconHelper::StyleColor styleColor;
    styleColor.defaultBorder = true;
    styleColor.position = "right";
    styleColor.iconSize = 1;
    styleColor.iconWidth = 12;
    styleColor.iconHeight = 2;
    styleColor.borderWidth = 4;
    IconHelper::setStyle(ui->wdg_left, btnGroup->buttons(), IconsLeft, styleColor);

    //默认选中某个按钮
    ui->btn_left_mtsn_f210->click();
}

// 左侧导航栏-f210原彩修复按钮
void MainWindow::on_btn_left_mtsn_f210_clicked()
{
    ui->te_prin->clear();
    Check_Port_Status_Each_Time();
    Check_new_device_detected(portStringList, PortList_f210, "CH340", ConnectStu, ui->te_prin);
    Check_null_device_detected(portStringList, "CH340", ui->te_prin);

    if(DevVersion.size() != 0)
    {
        //
        if(LangureStu == 1)
        {
            LangureStr = "Current equipment version number: "+DevVersion;
        }
        else
        {
            LangureStr = "当前设备版本号："+DevVersion;
        }
    }
    else
    {
        //
        if(LangureStu == 1)
        {
            LangureStr = "Current equipment version number: unrecognized";
        }
        else
        {
            LangureStr = "当前设备版本号：未识别";
        }
    }
    ui->lbl_dev_ver->setText(LangureStr);
    ui->cbx_upg->setVisible(true);
    ui->btn_upg->setVisible(true);
    ui->swdg->setCurrentWidget(ui->pg_color);
}
// 左侧导航栏-f210电池修复按钮
void MainWindow::on_btn_left_bat_f210_clicked()
{
    ui->te_prin_bat->clear();
    Check_Port_Status_Each_Time();
    Check_new_device_detected(portStringList, PortList_f210, "CH340", ConnectStu, ui->te_prin_bat);
    Check_null_device_detected(portStringList, "CH340", ui->te_prin_bat);

    ui->lbl_dev_ver->setText("");
    ui->swdg->setCurrentWidget(ui->pg_bat_f210);
}
// 左侧导航栏-f210显示修复按钮
void MainWindow::on_btn_left_disp_f210_clicked()
{
    ui->te_prin_disp_f210->clear();
    Check_Port_Status_Each_Time();
    Check_new_device_detected(portStringList, PortList_disp_f210, "CH340", ConnectStu_disp_f210, ui->te_prin_disp_f210);
    Check_null_device_detected(portStringList, "CH340", ui->te_prin_disp_f210);
    ui->lbl_dev_ver->setText("");
    ui->swdg->setCurrentWidget(ui->pg_disp_f210);
}
// 左侧导航栏-dl400按钮
void MainWindow::on_btn_left_mtsn_dlbox_clicked()
{
    ui->te_prin_dlbox->clear();
    Check_Port_Status_Each_Time();
    // dl400
    Check_new_device_detected(portStringList, PortList_dlbox, "Prolific", ConnectStu_dlbox, ui->te_prin_dlbox);
    Check_null_device_detected(portStringList, "Prolific", ui->te_prin_dlbox);
    ui->lbl_dev_ver->setText("");
    ui->cbx_upg->setVisible(false);
    ui->btn_upg->setVisible(false);
    ui->swdg->setCurrentWidget(ui->pg_mtsn_dlbox);
}
// 左侧导航栏-dl400动态配置
void MainWindow::on_btn_left_cfg_dlbox_clicked()
{
    ui->lbl_dev_ver->setText("");
    ui->swdg->setCurrentWidget(ui->pg_cfg_dlbox);
}

void MainWindow::on_btn_left_sql_dlbox_clicked()
{
    ui->lbl_dev_ver->setText("");
    ui->swdg->setCurrentWidget(ui->pg_sql_dlbox);
}
// 电池循环
void MainWindow::on_btn_left_loop_f316_clicked()
{
    if(DevVer_f316.size() != 0)
    {
        //
        if(LangureStu == 1)
        {
            LangureStr = "Current equipment version number: "+DevVer_f316;
        }
        else
        {
            LangureStr = "当前设备版本号："+DevVer_f316;
        }
    }
    else
    {
        //
        if(LangureStu == 1)
        {
            LangureStr = "Current equipment version number: unrecognized";
        }
        else
        {
            LangureStr = "当前设备版本号：未识别";
        }
    }
    ui->lbl_ver_f316->setText(LangureStr);
    ui->te_prin_f316->clear();
    Check_new_device_detected(portStringList, PortList_f316, "CH340", ConnStu_f316, ui->te_prin_f316);
    Check_null_device_detected(portStringList, "CH340", ui->te_prin_f316);
    ui->lbl_dev_ver->setText("");
    ui->swdg->setCurrentWidget(ui->pg_loop_f316);
}

// 原彩修复
void MainWindow::on_btn_left_dl8000_clicked()
{
    if(DevVer_dl8000.size() != 0)
    {
        //
        if(LangureStu == 1)
        {
            LangureStr = "Current equipment version number: "+DevVer_dl8000;
        }
        else
        {
            LangureStr = "当前设备版本号："+DevVer_dl8000;
        }
    }
    else
    {
        //
        if(LangureStu == 1)
        {
            LangureStr = "Current equipment version number: unrecognized";
        }
        else
        {
            LangureStr = "当前设备版本号：未识别";
        }
    }
    ui->lbl_ver_dl8000->setText(LangureStr);
    ui->te_prin_dl8000->clear();
    ui->lbl_dev_ver->setText("");
    ui->cbx_model_read_dl8000->setHidden(true);
    ui->lbl_model_read_dl8000->setHidden(true);
    ui->swdg->setCurrentWidget(ui->pg_mtsn_dl8000);
}
